import { MapUtils } from "@/utils/index";

/**
 * sessionStorage 缓存操作工具
 */
export const sessionCache = {
    /**
     * 设置 sessionStorage 项
     * @param key - 键
     * @param value - 值
     */
    set(key: string | null, value: string | null) {
        if (!sessionStorage) {
            return;
        }
        if (key != null && value != null) {
            sessionStorage.setItem(key, value);
        }
    },

    /**
     * 获取 sessionStorage 项
     * @param key - 键
     * @returns 值
     */
    get(key: string | null) {
        if (!sessionStorage) {
            return null;
        }
        if (key == null) {
            return null;
        }
        return sessionStorage.getItem(key);
    },

    /**
     * 设置 JSON 对象到 sessionStorage
     * @param key - 键
     * @param jsonValue - JSON 值
     */
    setJSON(key: string, jsonValue: { url: string | undefined; data: any; time: number } | null) {
        if (jsonValue != null) {
            this.set(key, JSON.stringify(jsonValue));
        }
    },

    /**
     * 从 sessionStorage 获取 JSON 对象
     * @param key - 键
     * @returns JSON 对象
     */
    getJSON(key: string) {
        const value = this.get(key);
        if (value != null) {
            return JSON.parse(value);
        }
        return null;
    },

    /**
     * 从 sessionStorage 移除项
     * @param key - 键
     */
    remove(key: string) {
        sessionStorage.removeItem(key);
    }
};

/**
 * localStorage 缓存操作工具
 */
export const localCache = {
    /**
     * 设置 localStorage 项
     * @param key - 键
     * @param value - 值
     */
    set(key: string | null, value: any | null) {
        if (!localStorage) {
            return;
        }
        if (key != null && value != null && typeof value === 'string') {
            localStorage.setItem(key + "-Type", "string");
            localStorage.setItem(key, value);
        } else if (key != null && value != null && typeof value === 'object' && value instanceof Map) {
            localStorage.setItem(key + "-Type", "Map");
            localStorage.setItem(key, MapUtils.strMapToArray(value));
        } else if (key != null && value != null && typeof value === 'object' && value instanceof Array) {
            localStorage.setItem(key + "-Type", "Array");
            localStorage.setItem(key, JSON.stringify(value));
        } else if (key != null && value != null && typeof value === 'object' && value instanceof Object) {
            localStorage.setItem(key + "-Type", "Object");
            localStorage.setItem(key, JSON.stringify(value));
        }
    },

    /**
     * 获取 localStorage 项
     * @param key - 键
     * @returns 值
     */
    get(key: string | null) {
        if (!localStorage) {
            return null;
        }
        if (key == null) {
            return null;
        }
        return localStorage.getItem(key + "-Type") !== undefined && localStorage.getItem(key + "-Type") === 'Map'
            ? MapUtils.strArrayToMap(JSON.parse(localStorage.getItem(key) ?? "[]"))
            : (
                localStorage.getItem(key + "-Type") !== undefined && localStorage.getItem(key + "-Type") === 'Object'
                    ? JSON.parse(localStorage.getItem(key) ?? "{}")
                    : (
                        localStorage.getItem(key + "-Type") !== undefined && localStorage.getItem(key + "-Type") === 'Array'
                            ? JSON.parse(localStorage.getItem(key) ?? "[]")
                            : localStorage.getItem(key)
                    )
            );
    },

    /**
     * 设置 JSON 对象到 localStorage
     * @param key - 键
     * @param jsonValue - JSON 值
     */
    setJSON(key: string, jsonValue: any) {
        if (jsonValue != null) {
            this.set(key, JSON.stringify(jsonValue));
        }
    },

    /**
     * 从 localStorage 获取 JSON 对象
     * @param key - 键
     * @returns JSON 对象
     */
    getJSON(key: string) {
        const value = this.get(key);
        if (value != null) {
            return JSON.parse(value);
        }
        return null;
    },

    /**
     * 从 localStorage 移除项
     * @param key - 键
     */
    remove(key: string) {
        localStorage.removeItem(key);
    }
};